package cn.echoedu.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.AClass;
import cn.echoedu.entity.ClassNotify;
import cn.echoedu.entity.Department;
import cn.echoedu.entity.ReceiveClassNotify;
import cn.echoedu.entity.User;
import cn.echoedu.entity.receivecommand.ClassNotifyListReceive;
import cn.echoedu.entity.receivecommand.MonitorAddNotifyReceive;
import cn.echoedu.util.L;

public class ReceiveClassNotifyModel extends BaseModel{
	private final String T = "ReceiveClassNotifyModel----->";
	public List<ReceiveClassNotify> whenStudentQuery(
			ClassNotifyListReceive classnotifyreceive) {
		// TODO Auto-generated method stub
		final String M = "whenStudentQuery************>>";
		return queryBySql(getStudentQuerySql(classnotifyreceive));
	}


	private List<ReceiveClassNotify> queryBySql(String sql) {
		// TODO Auto-generated method stub
		final String M = "queryBySql***********>>";
		L.p(T+M+sql);
		List<ReceiveClassNotify> receiveclassnotifylist = null;
		try {
			receiveclassnotifylist = getDAORead().getPojoForSql(getConnection(), TableName.RECEIVE_CLASS_NOTIFY_TABLE,
					sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return receiveclassnotifylist;
	}


	private String getStudentQuerySql(ClassNotifyListReceive classnotifyreceive) {
		// TODO Auto-generated method stub
		return "select * from receiveclassnotify where receiveusernumber='"
		+classnotifyreceive.getUser_number()+"'or receiveclasscode='"
		+classnotifyreceive.getAclasscode()+"' order by classnotifyid desc limit "
		+(classnotifyreceive.getEndindex()-classnotifyreceive.getBeginindex());
	}


	public List<ReceiveClassNotify> whenTeacherQuery(
			ClassNotifyListReceive classnotifyreceive) {
		// TODO Auto-generated method stub
		final String M = "whenTeacherQuery********>>";
		String sql = getWhenTeacherQuerySql(classnotifyreceive);
		return queryBySql(sql);
	}

	private String getWhenTeacherQuerySql(
			ClassNotifyListReceive classnotifyreceive) {
		// TODO Auto-generated method stub
		return "select * from receivecalssnotify where receiveusernumber='"
		+classnotifyreceive.getUser_number()+" or receivedepartmentcode='"
		+classnotifyreceive.getDepartmentcode()
		+" order by receivenotifyid desc limit "
		+(classnotifyreceive.getEndindex()-classnotifyreceive.getBeginindex());
	}


	public boolean putInfoWhenDefault(
			MonitorAddNotifyReceive monitoraddnotifyreceive, int notifykey) {
		// TODO Auto-generated method stub
		ReceiveClassNotify receiveclassnotify = new ReceiveClassNotify();
		AClass aclass = new AClass();
		aclass.setClasscode(monitoraddnotifyreceive.getAclasscode());
		receiveclassnotify.setReceiveclass(aclass);
		ClassNotify classnotify = new ClassNotify();
		classnotify.setClassnotify(notifykey);
		receiveclassnotify.setClassnotify(classnotify);
		Department department = new Department();
		department.setDepartmentcode(monitoraddnotifyreceive.getDepartmentcode());
		receiveclassnotify.setDepartment(department);
		receiveclassnotify.setIsdefault(monitoraddnotifyreceive.getIsdefault());
		return  putInfoByReceiveClassNotify(receiveclassnotify);
	}
	private boolean isLess(int index,int size){
		return index<size?true:false;
	}
	public List<Integer> putInfoWhenIsNotDefault(
			MonitorAddNotifyReceive monitoraddnotifyreceive, int notifykey) {
		// TODO Auto-generated method stub
		ClassNotify classnotify = new ClassNotify();
		Department department = new Department();
		department.setDepartmentcode(monitoraddnotifyreceive.getDepartmentcode());
		classnotify.setClassnotify(notifykey);
		AClass aclass = new AClass();
//		aclass.setClasscode(monitoraddnotifyreceive.getAclasscode());
        List<ReceiveClassNotify> receiveclassnotifylist = new ArrayList<ReceiveClassNotify>();
		for(int userindex=0;
				isLess(userindex,monitoraddnotifyreceive.getReceive_person_list().size())
				;userindex++){
			L.p("receiveperson"+monitoraddnotifyreceive.getReceive_person_list()
					.get(userindex).getUser_number());
			ReceiveClassNotify receiveclassnotify = new ReceiveClassNotify();
			receiveclassnotify.setClassnotify(classnotify);
			receiveclassnotify.setIsdefault(1);
			receiveclassnotify.setDepartment(department);
			receiveclassnotify.setReceiveclass(aclass);
			User user = new User();
			String usernumber = monitoraddnotifyreceive
					.getReceive_person_list()
					.get(userindex).getUser_number();
			if(usernumber!=null){
				user.setUsernumber(usernumber);
			}
			receiveclassnotify.setUser(user);
			receiveclassnotifylist.add(receiveclassnotify);
		}
		return putListInfoByReceiveClassNotifyList(receiveclassnotifylist);
		 
	}
	private List<Integer> putListInfoByReceiveClassNotifyList(
			List<ReceiveClassNotify> receiveclassnotifylist) {
		// TODO Auto-generated method stub
		try {
			return getDAOInsert().putListInfo(getConnection(), 
					TableName.RECEIVE_CLASS_NOTIFY_TABLE,
					receiveclassnotifylist,true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	private boolean putInfoByReceiveClassNotify(ReceiveClassNotify receiveclassnotify){
		try {
			return getDAOInsert().putInto(getConnection(), 
					TableName.RECEIVE_CLASS_NOTIFY_TABLE,
					receiveclassnotify,true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}


	public boolean  deleteByClassNotifyid(int classnotifyid) {
		// TODO Auto-generated method stub
		return getDAODelete().deleteByColumnFrom(getConnection(),
				TableName.RECEIVE_CLASS_NOTIFY_TABLE,"classnotifyid", classnotifyid+"");
	}
}
